<?php
declare (strict_types=1);

namespace app\user\logic\user;

use app\circle\logic\Circle;
use app\common\exception\LogicException;
use app\common\model\{BaseModel};
use app\course\logic\base\{Course, CourseStudent};
use app\user\logic\teacher\Teacher;
use app\user\validate\user\Home as HomeValidate;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\Model;

/**
 * 个人主页类
 */
class Home
{

    /**
     * 数据验证
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-16
     *
     * @param string $scene
     * @param array  $param
     * @return void [type] [description]
     */
    public function checkValidate(string $scene, array $param)
    {
        try {
            validate(HomeValidate::class)->scene($scene)->check($param);
        } catch (ValidateException $e) {
            throw new LogicException($e->getError());
        }
    }

    /**
     * 主页基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-16
     *
     * @param array $param
     * @return Teacher|Account|array|Model [type] [description]
     */
    public function base(array $param)
    {
        // 数据验证
        $this->checkValidate('base', $param);
        // 参数
        $userId = intval($param['user_id']);
        if (!in_array(request()->device_type, ['ios', 'android'])) {
            // 判断讲师
            $teacherId = app(Teacher::class)->isTeacher($userId, true);
            if ($teacherId) return app(Teacher::class)->getBasicInfo($teacherId);
        }
        // 用户基础信息
        $user = app(Account::class)->field('id,nick_name,signature,avatar')->with(['avatar_url'])->withCount([
            'has_been_followed'  => 'followed',
            'following_num'      => 'following_num',
            'fans_num'           => 'fans_num',
            'recent_visitor_num' => 'recent_visitor_num',
        ])->findOrEmpty($userId);

        // 添加访客
        if (request()->mid && $userId != request()->mid) app(Visitor::class)->addVisitor($userId);
        // 身份
        $user->identity = 1;

        return $user;
    }

    /**
     * 主页动态列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-16
     *
     * @param array $param
     * @return mixed [type] [description]
     */
    public function circle(array $param)
    {
        // 数据验证
        $this->checkValidate('circle', $param);
        // 参数
        $user_id = intval($param['user_id']);
        // 列表
        return app(Circle::class)->getMyCircleList($user_id);
    }

    /**
     * 主页基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-16
     *
     * @param array $param
     * @return mixed [type] [description]
     * @throws DbException
     */
    public function course(array $param)
    {
        // 数据验证
        $this->checkValidate('course', $param);
        // 参数
        $user_id = intval($param['user_id']);
        $type    = intval($param['type']);
        // 列表
        $ids  = app(CourseStudent::class)
            ->alias('stu')
            ->join('course c', 'c.id=stu.course_id')
            ->where('stu.user_id', $user_id)
            ->where('c.course_type', $type)
            ->column('c.id');
        return app(Course::class)
            ->field('id,title,cover,price,sale_count,sale_count_market')
            ->with(['cover_url'])
            // ->withCount(['student'])
            ->whereIn('id', $ids)
            ->paginate(BaseModel::getListRows())
            ->append([])
            ->each(function ($i) {
                $i->student_count = $i->sale_count;
            });
    }

}
